package com.shengqi.xinnongren.work.ui.goods.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.utils.ImageUtils;
import com.shengqi.xinnongren.work.ui.goods.model.GoodsBanner;

import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

public class BannerPagerAdapter extends PagerAdapter {
    private List<GoodsBanner> mBannerList;
    private Context mContext;

    public BannerPagerAdapter(List<GoodsBanner> bannerList, Context context) {
        mBannerList = bannerList;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mBannerList.size() == 0 ? 0 : Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        position = mBannerList.size() == 0 ? 0 : position % mBannerList.size();
        View layout = LayoutInflater.from(mContext).inflate(R.layout.item_goods_banner, null);
        ImageView imageView = layout.findViewById(R.id.iv_picture);
        JZVideoPlayer videoPlayer = layout.findViewById(R.id.videoplayer);
        container.addView(layout);
        GoodsBanner banner = mBannerList.get(position);
        if (banner.isVideo()) {
            videoPlayer.setVisibility(View.VISIBLE);
            videoPlayer.setUp(banner.url, JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, "");
        } else {
            videoPlayer.setVisibility(View.GONE);
            ImageUtils.showImage(mContext, mBannerList.get(position).url, imageView);
        }
        return layout;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}